package gui;

import java.awt.BorderLayout;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JPanel;

import operations.SW;

import languages.Language;
import layers.ArcLayer;
import layers.NodeLayer;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.Layer;
import com.bbn.openmap.LayerHandler;
import com.bbn.openmap.MapBean;
import com.bbn.openmap.MapHandler;
import com.bbn.openmap.MouseDelegator;
import com.bbn.openmap.event.PanMouseMode;
import com.bbn.openmap.event.SelectMouseMode;
import com.bbn.openmap.gui.OMToolSet;
import com.bbn.openmap.layer.shape.ShapeLayer;
import com.bbn.openmap.tools.drawing.OMDrawingTool;
import com.bbn.openmap.tools.drawing.OMDrawingToolLauncher;
import com.bbn.openmap.tools.drawing.OMDrawingToolMouseMode;
import com.bbn.openmap.tools.drawing.OMPointLoader;
import com.bbn.openmap.tools.drawing.OMPolyLoader;

import components.ArcList;
import components.NodeList;
import draw.DrawPoint;
import draw.DrawPoly;

public class Window extends JFrame{
	
	private Language language;
	
	private MapHandler mapHandler;
	private MapBean mapBean;
	private MouseDelegator mouseDelegator;
	
	private JPanel layeredPane;
	
	private NodeLayer nodeLayer;
	private ArcLayer arcLayer;
	
	private ArcList arcs;
	private NodeList nodes;
	
	private Menu menu;
	
	private DrawPoint drawPoint;
	private DrawPoly drawPoly;
	
	private SW sw;
 
	public Window(Properties prop, Language lang) {
		super(prop.getProperty("main.appName"));
		
		language = lang;
		
		sw = new SW(this,prop);
		
		this.getContentPane().setLayout(new BorderLayout());
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		mapHandler = new MapHandler();
		mapBean = new MapBean();
		
		mapBean.setScale(9000000f);
		mapBean.setCenter(new LatLonPoint(52f, 18.5f));
		
		mapHandler.add(mapBean);
		this.getContentPane().add(BorderLayout.CENTER, mapBean);
		
		mapHandler.add(this);
		
		//add mouse modes
		mouseDelegator = new MouseDelegator();
		mapHandler.add(mouseDelegator);
		mapHandler.add(new PanMouseMode());
		mapHandler.add(new SelectMouseMode());
        mapHandler.add(new OMDrawingToolMouseMode());

        //add drawing tools properties
        mapHandler.add(new OMDrawingTool());
        mapHandler.add(new OMPointLoader());
        mapHandler.add(new OMPolyLoader());
		
		mapHandler.add(new LayerHandler());
		
		drawPoint = new DrawPoint(this);
		mapHandler.add(drawPoint);
		
		drawPoly = new DrawPoly(this);
		mapHandler.add(drawPoly);
		
		Layer layer = createBasicLayer(prop);
		if(layer != null){
			mapHandler.add(layer);
		}
		
		
		nodes = new NodeList(prop.getProperty("nodes.file"));
		ArcList arcs = new ArcList(nodes,this);
		nodeLayer = new NodeLayer(nodes,this);
		if(nodeLayer != null){
			nodeLayer.setName("Nodes");
			mapHandler.add(nodeLayer);
		}
		
		arcs = new ArcList(nodes,prop.getProperty("arcs.file"),this);
		arcLayer = new ArcLayer(arcs,this);
		if(arcLayer != null){
			arcLayer.setName("Arcs");
			mapHandler.add(arcLayer);
		}
		
		
		//add menu
		menu = new Menu(language,this);
		this.getContentPane().add(BorderLayout.NORTH, menu);
		mapHandler.add(menu);
		
		//add navigation tools
		Navigation nav = new Navigation(this,language);
		
		mapHandler.add(nav);
		mapHandler.add(new OMToolSet());
		
		this.getContentPane().add(BorderLayout.WEST, nav);
		
	}

	private Layer createBasicLayer(Properties prop) {
		ShapeLayer basic = new ShapeLayer();
		basic.setProperties("basic",prop);
		return basic;
	}
	
	public MapHandler getMapHandler(){
		return mapHandler;
	}
	
	public MapBean getMapBean(){
		return mapBean;
	}

	public NodeLayer getNodeLayer() {
		return nodeLayer;
	}

	public ArcLayer getArcLayer() {
		return arcLayer;
	}
	
	public MouseDelegator getMouseDelegator(){
		return mouseDelegator;
	}
	
	public Menu getMenu(){
		return menu;
	}
	
	public DrawPoint getDrawPoint(){
		return drawPoint;
	}
	
	public DrawPoly getDrawPoly(){
		return drawPoly;
	}
	
	public NodeList getNodeList(){
		return nodes;
	}
	
	public ArcList getArcList(){
		return arcs;
	}
	
	public SW getSW(){
		return sw;
	}

}
